var doc_gen = function(papername,schoolname,date){
    const officegen = require('officegen');
    const xuanze = require('../db/xuanze.js')
    const fs = require('fs');
    
    var docx = officegen({
        type:'docx',
        author:schoolname,
        title:papername
    });
    let titlep0bj = docx.createP();
    let namep0bj = docx.createP();
    let datep0bj = docx.createP();
    titlep0bj.options.align='center';
    titlep0bj.addText(papername);
    titlep0bj.addLineBreak;
    namep0bj.options.align='center';
    namep0bj.addText(schoolname);
    namep0bj.addLineBreak;
    datep0bj.options.align='center';
    datep0bj.addText(date);
    datep0bj.addLineBreak;
    for (var i = 1; i <= 10; i++) {
        xuanze.find({ Tno: i }, { Qes: 1,Ans1:1,Ans2:1,Ans3:1,Ans4:1 }, function (err, data) {
            if (err) {
                console.log(err);
                return;
            }
            var ques = data;
            let p0bj = new docx.createP();
            p0bj.addText(ques[0].Qes);
            p0bj.addLineBreak();
            p0bj.addText(ques[0].Ans1);
            p0bj.addLineBreak();
            p0bj.addText(ques[0].Ans2);
            p0bj.addLineBreak();
            p0bj.addText(ques[0].Ans3);
            p0bj.addLineBreak();
            p0bj.addText(ques[0].Ans4);
            p0bj.addLineBreak();
        })
    }
    
    setTimeout(() => {
        var out = fs.createWriteStream('../static/docs/'+papername+'.docx');
        out.on('close', () => {
            console.log('success');
        })
        docx.generate(out);
    }, 3000)
}
module.exports=doc_gen;